package com.fr.base.core.antlr;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class PreservingFileWriter extends FileWriter
{
  protected File target_file;
  protected File tmp_file;

  public PreservingFileWriter(String paramString)
    throws IOException
  {
    super(paramString + ".antlr.tmp");
    this.target_file = new File(paramString);
    String str = this.target_file.getParent();
    if (str != null)
    {
      File localFile = new File(str);
      if (!(localFile.exists()))
        throw new IOException("destination directory of '" + paramString + "' doesn't exist");
      if (!(localFile.canWrite()))
        throw new IOException("destination directory of '" + paramString + "' isn't writeable");
    }
    if ((this.target_file.exists()) && (!(this.target_file.canWrite())))
      throw new IOException("cannot write to '" + paramString + "'");
    this.tmp_file = new File(paramString + ".antlr.tmp");
  }

  public void close()
    throws IOException
  {
    BufferedReader localBufferedReader1 = null;
    BufferedWriter localBufferedWriter = null;
    try
    {
      super.close();
      char[] arrayOfChar1 = new char[1024];
      if (this.target_file.length() == this.tmp_file.length())
      {
        char[] arrayOfChar2 = new char[1024];
        localBufferedReader1 = new BufferedReader(new FileReader(this.tmp_file));
        BufferedReader localBufferedReader2 = new BufferedReader(new FileReader(this.target_file));
        int l = 1;
        if (l != 0)
        {
          int j = localBufferedReader1.read(arrayOfChar1, 0, 1024);
          int k = localBufferedReader2.read(arrayOfChar2, 0, 1024);
          if (j != k)
          {
            l = 0;
          }
          else
          {
            if (j == -1)
              break label166:
            int i1 = 0;
            while (true)
            {
              while (true)
              {
                if (i1 >= j);
                if (arrayOfChar1[i1] == arrayOfChar2[i1])
                  break;
                l = 0;
              }
              ++i1;
            }
          }
        }
        label166: localBufferedReader1.close();
        localBufferedReader2.close();
        localBufferedReader1 = localBufferedReader2 = null;
        if (l != 0)
        {
          jsr 87;
          return;
        }
      }
      localBufferedReader1 = new BufferedReader(new FileReader(this.tmp_file));
      localBufferedWriter = new BufferedWriter(new FileWriter(this.target_file));
      while (true)
      {
        int i = localBufferedReader1.read(arrayOfChar1, 0, 1024);
        if (i == -1)
          break;
        localBufferedWriter.write(arrayOfChar1, 0, i);
      }
    }
    finally
    {
      if (localBufferedReader1 != null)
        try
        {
          localBufferedReader1.close();
        }
        catch (IOException localIOException1)
        {
        }
      if (localBufferedWriter != null)
        try
        {
          localBufferedWriter.close();
        }
        catch (IOException localIOException2)
        {
        }
      if ((this.tmp_file != null) && (this.tmp_file.exists()))
      {
        this.tmp_file.delete();
        this.tmp_file = null;
      }
    }
  }
}